#ifndef NEWSPAPERMANAGER_H
#define NEWSPAPERMANAGER_H

#include "network/networkmanager.h"
#include "network/networkrequest.h"
#include "newspaper/sourcedetail.h"

#include <QObject>
#include <QList>
#include <QMap>

class DPIScreen;
class CategoryPaper;
class SourceCategoryInfo;
class Article;
class CategoryDetail;
class ArticleListBySource;

namespace Network {
class HttpRequest;
}

class NewspaperManager : public QObject
{
    Q_OBJECT
public:
    explicit NewspaperManager(QObject *parent = 0);

    static NewspaperManager *instance(QObject *parent = 0);
    static int ID_SOURCE_TONG_HOP;
    static int ID_CATEGORY_TONG_HOP;

    void init();

    inline QString dpiScreen() const {
        return m_stringDPI;
    }

    inline QString simageDomain() const {
        return m_simageDomain;
    }


    inline QList < CategoryDetail* > listCategoryDetail() const {
        return m_currentSourceDetail->listCategoryDetail();
    }


    inline int getSizeCategorysCurrentSource(){
        return m_currentSourceDetail->sizeListCategory();
    }


    bool findExistCategory(int categoryId);
    inline int getCategoryIdByIndexCurrentSource(const int idx)
    {
        return m_currentSourceDetail->getCategoryIdByIndex(idx);
    }

    CategoryDetail* findCategoryDetail(int cid);
    SourceInfo getSourceInfo(const int sid);

    QList<SourceInfo> getListSourceInfo() const;
    QList<SourceInfo> getListSourceInfoDefault();

signals:
    void addArticleListByCategoryChanged(CategoryDetail* articleListByCategory);
    void addArticleListByCategoryMoreChanged(CategoryDetail* articleListByCategory,
                                             int cid);
    void addArticleListByCategoryNewChanged(CategoryDetail* articleListByCategory,
                                            int cid);
    void clearArticleListByCategoryChanged(int indexCategory,
                                           int cid);
    void addArticleListBySourceChanged();
    void updateArticleContentSuccessFull( Article* article);

    void finishGetCategorySourceInfo();

    void isHasUpdateMoreArticle(bool check);

    void hasDpiScreen(QString dpi);

    void openingUpdateSuccess();


private slots:
    void onOpeningUpdateAppResponsed(Network::HttpRequest *request);
    void onUpdateArticleListByCategoryResponsed(Network::HttpRequest *request);
    void onUpdateMoreArticleListByCategoryResponsed(Network::HttpRequest *request);
    void onUpdateNewArticleListByCategoryResponsed(Network::HttpRequest *request);
    void onUpdateArticleListBySourceResponsed(Network::HttpRequest *request);
    void onUpdateArticleContentResponsed(Network::HttpRequest *request);

public:
    void getUpdateMoreCategory(int cid); //
    void getUpdateAticleContent(int categoryId, int articleId);
    void getUpdateMoreArticle(int categoryId);
    void getUpdateNewArticle(int categoryId);
    void changeCurrentSourceDetail(const int sourceId);
private:
    void getDPIScreen();
    void getOpeningUpdateApp();

    void getFirstUpdateArticleList();
    void getUpdateArticleListByCategory( int cid, int firstId);
    void getUpdateMoreArticleListByCategory( int cid, int lastId);
    void getUpdateNewArticleListByCategory( int cid, int lastId);
    // lan dau tien tao 1 trang
    void getUpdateArticleListByCategory( int userId,
                                         int sid,
                                         int cid,
                                         int count,
                                         int articleInfoCount,
                                         int firstId);
    // them data vao cuoi cua 1 trang
    void getUpdateMoreArticleListByCategory( int userId,
                                             int sid,
                                             int cid,
                                             int count,
                                             int articleInfoCount,
                                             int lastId);
    // them data vao dau cua 1 trang
    void getUpdateNewArticleListByCategory( int userId,
                                            int sid,
                                            int cid,
                                            int count,
                                            int articleInfoCount,
                                            int lastId);

private:
    SourceDetail* getSourceDetailById(const int sourceId);
    SourceDetail* getSourceDetailByIndex(const int idx);

public:

    void getUpdateArticleContent(Article *article);

    static NewspaperManager *m_instance;

    DPIScreen* m_dpiScreen;
    QString m_stringDPI;

    QString m_simageDomain;


private:

    QList < int > m_listSourceIdDefault;
    SourceDetail* m_currentSourceDetail;
    QMap<int, SourceDetail*> m_mapId2SourceDetail;
    QList<SourceDetail*> m_listSourceDetail;




};

#endif // NEWSPAPERMANAGER_H
